From e84a523750ba14bafcbba1d1d584edbb1fcfc52f Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 15 May 2014 19:34:38 -0400 Subject: [PATCH] inspector: Show resources Show a list of all registered resources. If a resource looks like text or an image, we show its content. --- gtk/inspector/Makefile.am | 3 + gtk/inspector/init.c | 2 + gtk/inspector/inspector.gresource.xml | 1 + gtk/inspector/resource-list.c | 172 ++++++++++++++++++++++++++ gtk/inspector/resource-list.h | 52 ++++++++ gtk/inspector/resource-list.ui | 92 ++++++++++++++ gtk/inspector/window.ui | 11 ++ po/POTFILES.in | 1 + 8 files changed, 334 insertions(+) create mode 100644 gtk/inspector/resource-list.c create mode 100644 gtk/inspector/resource-list.h create mode 100644 gtk/inspector/resource-list.ui diff --git a/gtk/inspector/Makefile.am b/gtk/inspector/Makefile.am index 2e50840f02..a5c778c12e 100644 --- a/gtk/inspector/Makefile.am +++ b/gtk/inspector/Makefile.am @@ -40,6 +40,8 @@ libgtkinspector_la_SOURCES = \ python-hooks.c \ python-shell.h \ python-shell.c \ + resource-list.h \ + resource-list.c \ resources.h \ resources.c \ signals-list.h \ @@ -84,6 +86,7 @@ templates = \ general.ui \ object-hierarchy.ui \ prop-list.ui \ + resource-list.ui \ signals-list.ui \ visual.ui \ widget-tree.ui \ diff --git a/gtk/inspector/init.c b/gtk/inspector/init.c index 4ea5b759a5..c3cae4f361 100644 --- a/gtk/inspector/init.c +++ b/gtk/inspector/init.c @@ -33,6 +33,7 @@ #include "prop-list.h" #include "python-hooks.h" #include "python-shell.h" +#include "resource-list.h" #include "resources.h" #include "signals-list.h" #include "visual.h" @@ -58,6 +59,7 @@ gtk_inspector_init (void) g_type_ensure (GTK_TYPE_INSPECTOR_PROPERTY_CELL_RENDERER); g_type_ensure (GTK_TYPE_INSPECTOR_PROP_LIST); g_type_ensure (GTK_TYPE_INSPECTOR_PYTHON_SHELL); + g_type_ensure (GTK_TYPE_INSPECTOR_RESOURCE_LIST); g_type_ensure (GTK_TYPE_INSPECTOR_SIGNALS_LIST); g_type_ensure (GTK_TYPE_INSPECTOR_VISUAL); g_type_ensure (GTK_TYPE_INSPECTOR_WIDGET_TREE); diff --git a/gtk/inspector/inspector.gresource.xml b/gtk/inspector/inspector.gresource.xml index a9e3f72540..427cb02ef3 100644 --- a/gtk/inspector/inspector.gresource.xml +++ b/gtk/inspector/inspector.gresource.xml @@ -9,6 +9,7 @@ general.ui object-hierarchy.ui prop-list.ui + resource-list.ui signals-list.ui visual.ui widget-tree.ui diff --git a/gtk/inspector/resource-list.c b/gtk/inspector/resource-list.c new file mode 100644 index 0000000000..77278203b7 --- /dev/null +++ b/gtk/inspector/resource-list.c @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2014 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include "config.h" +#include +#include "resource-list.h" + +enum +{ + COLUMN_NAME, + COLUMN_PATH +}; + +struct _GtkInspectorResourceListPrivate +{ + GtkTreeStore *model; + GtkTextBuffer *buffer; + GtkWidget *image; + GtkWidget *content; +}; + +G_DEFINE_TYPE_WITH_PRIVATE (GtkInspectorResourceList, gtk_inspector_resource_list, GTK_TYPE_BOX) + +static void +load_resources_recurse (GtkInspectorResourceList *sl, + GtkTreeIter *parent, + const gchar *path) +{ + gchar **names; + gint i; + GtkTreeIter iter; + + names = g_resources_enumerate_children (path, 0, NULL); + for (i = 0; names[i]; i++) + { + gint len; + gchar *p; + gboolean has_slash; + + p = g_strconcat (path, names[i], NULL); + + len = strlen (names[i]); + has_slash = names[i][len - 1] == '/'; + + if (has_slash) + names[i][len - 1] = '\0'; + + gtk_tree_store_append (sl->priv->model, &iter, parent); + gtk_tree_store_set (sl->priv->model, &iter, + COLUMN_NAME, names[i], + COLUMN_PATH, p, + -1); + + if (has_slash) + load_resources_recurse (sl, &iter, p); + + g_free (p); + } + g_strfreev (names); + +} + +static void +selection_changed (GtkTreeSelection *selection, + GtkInspectorResourceList *rl) +{ + GtkTreeIter iter; + + if (gtk_tree_selection_get_selected (selection, NULL, &iter)) + { + gchar *path; + GBytes *bytes; + gchar *type; + gconstpointer data; + gsize size; + GError *error = NULL; + + gtk_tree_model_get (GTK_TREE_MODEL (rl->priv->model), &iter, + COLUMN_PATH, &path, + -1); + if (g_str_has_suffix (path, "/")) + { + gtk_text_buffer_set_text (rl->priv->buffer, "", -1); + gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text"); + goto out; + } + bytes = g_resources_lookup_data (path, 0, &error); + if (bytes == NULL) + { + gtk_text_buffer_set_text (rl->priv->buffer, error->message, -1); + g_error_free (error); + gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text"); + } + else + { + data = g_bytes_get_data (bytes, &size); + type = g_content_type_guess (NULL, data, size, NULL); + if (g_content_type_is_a (type, "text/*")) + { + gtk_text_buffer_set_text (rl->priv->buffer, data, -1); + gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text"); + } + else if (g_content_type_is_a (type, "image/*")) + { + gtk_image_set_from_resource (GTK_IMAGE (rl->priv->image), path); + gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "image"); + } + else + { + gchar *content; + content = g_content_type_get_description (type); + gtk_text_buffer_set_text (rl->priv->buffer, content, -1); + g_free (content); + gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text"); + } + g_free (type); + g_bytes_unref (bytes); + } +out: + g_free (path); + } + else + { + gtk_text_buffer_set_text (rl->priv->buffer, "", -1); + gtk_stack_set_visible_child_name (GTK_STACK (rl->priv->content), "text"); + } +} + +static void +load_resources (GtkInspectorResourceList *sl) +{ + + load_resources_recurse (sl, NULL, "/"); +} + +static void +gtk_inspector_resource_list_init (GtkInspectorResourceList *sl) +{ + sl->priv = gtk_inspector_resource_list_get_instance_private (sl); + gtk_widget_init_template (GTK_WIDGET (sl)); + load_resources (sl); +} + +static void +gtk_inspector_resource_list_class_init (GtkInspectorResourceListClass *klass) +{ + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/inspector/resource-list.ui"); + gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, model); + gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, buffer); + gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, content); + gtk_widget_class_bind_template_child_private (widget_class, GtkInspectorResourceList, image); + + gtk_widget_class_bind_template_callback (widget_class, selection_changed); +} + +// vim: set et sw=2 ts=2: diff --git a/gtk/inspector/resource-list.h b/gtk/inspector/resource-list.h new file mode 100644 index 0000000000..2cc0beb52f --- /dev/null +++ b/gtk/inspector/resource-list.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2014 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#ifndef _GTK_INSPECTOR_RESOURCE_LIST_H_ +#define _GTK_INSPECTOR_RESOURCE_LIST_H_ + +#include + +#define GTK_TYPE_INSPECTOR_RESOURCE_LIST (gtk_inspector_resource_list_get_type()) +#define GTK_INSPECTOR_RESOURCE_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceList)) +#define GTK_INSPECTOR_RESOURCE_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceListClass)) +#define GTK_INSPECTOR_IS_RESOURCE_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GTK_TYPE_INSPECTOR_RESOURCE_LIST)) +#define GTK_INSPECTOR_IS_RESOURCE_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GTK_TYPE_INSPECTOR_RESOURCE_LIST)) +#define GTK_INSPECTOR_RESOURCE_LIST_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GTK_TYPE_INSPECTOR_RESOURCE_LIST, GtkInspectorResourceListClass)) + + +typedef struct _GtkInspectorResourceListPrivate GtkInspectorResourceListPrivate; + +typedef struct _GtkInspectorResourceList +{ + GtkBox parent; + GtkInspectorResourceListPrivate *priv; +} GtkInspectorResourceList; + +typedef struct _GtkInspectorResourceListClass +{ + GtkBoxClass parent; +} GtkInspectorResourceListClass; + +G_BEGIN_DECLS + +GType gtk_inspector_resource_list_get_type (void); + +G_END_DECLS + +#endif // _GTK_INSPECTOR_RESOURCE_LIST_H_ + +// vim: set et sw=2 ts=2: diff --git a/gtk/inspector/resource-list.ui b/gtk/inspector/resource-list.ui new file mode 100644 index 0000000000..a7226b9873 --- /dev/null +++ b/gtk/inspector/resource-list.ui @@ -0,0 +1,92 @@ + + + + + + + + + + + + + diff --git a/gtk/inspector/window.ui b/gtk/inspector/window.ui index 7e2687024b..89ec579808 100644 --- a/gtk/inspector/window.ui +++ b/gtk/inspector/window.ui @@ -231,6 +231,17 @@ Custom CSS + + + True + + + + + True + Resources + + True diff --git a/po/POTFILES.in b/po/POTFILES.in index 3aa86b223e..d7f4399d54 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -279,6 +279,7 @@ gtk/inspector/general.ui.h gtk/inspector/inspect-button.c gtk/inspector/object-hierarchy.ui.h gtk/inspector/prop-list.ui.h +gtk/inspector/resource-path.ui.h gtk/inspector/signals-list.c gtk/inspector/signals-list.ui.h gtk/inspector/visual.ui.h -- 2.30.2